package com.arvin.service.serviceImpl;

import com.arvin.mapper.ExcelImportMapper;
import com.arvin.pojo.ExamInfo;
import com.arvin.pojo.IntvInfo;
import com.arvin.pojo.Result;
import com.arvin.service.ExcelImportService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExcelImportServceImpl implements ExcelImportService {

    @Autowired
    private ExcelImportMapper excelImportMapper;

    /**
     * 导入execl文件批量更新考试分数
     *
     * @param file
     * @return
     * @throws IOException
     */
    public Result excelImportExam(MultipartFile file) throws IOException {
        List<ExamInfo> examInfoList = new ArrayList<>();
        //根据路径获取这个操作excel表格
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        //根据页面index 获取sheet页
        XSSFSheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 2; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            List<Object> list = null;
            if (row != null) {
                list = new ArrayList<>();
                for (Cell cell : row) {
                    if (cell != null) {
                        if (CellType.STRING.equals(cell.getCellType())) {
                            cell.setCellType(CellType.STRING);
                            String value = cell.getStringCellValue();
                            if (value != null && !"".equals(value))
                                list.add(value);
                        }
                        else{
                            cell.setCellType(CellType.NUMERIC);
                            Integer value = (int) cell.getNumericCellValue();
                            if (value != null && !"".equals(value))
                                list.add(value);
                        }
                        if (row.getCell(9).getCellType()!=CellType.NUMERIC){
                            return Result.error("上传失败，第"+i+"行第9列数据格式不对,应当为数字类型");
                        }
                    }
                }

                if (!list.isEmpty() && row.getCell(0) != null) {
                    ExamInfo examInfo = new ExamInfo();
                    try {
                        examInfo.setStudentId(row.getCell(0).getStringCellValue());
                        if (!StringUtils.isEmpty(row.getCell(8).getStringCellValue()))
                            if (!StringUtils.isEmpty((int) row.getCell(9).getNumericCellValue()))
                                examInfo.setExamState("已考试");
                        if (!StringUtils.isEmpty((int) row.getCell(9).getNumericCellValue()))
                            examInfo.setExamScore((int) row.getCell(9).getNumericCellValue());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    examInfoList.add(examInfo);
                    System.out.println(examInfoList);
                }
            }
        }
        for (ExamInfo examInfo:examInfoList){
            excelImportMapper.updateExamScoreById(examInfo);//通过Id更新
        }
        /*List<List<ExamInfo>> divide = ListUtils.divide(listList,2); // 获取：List 拆分为两个
        ExecutorService pool = Executors.newFixedThreadPool(2); // 开启俩个线程
        //创建CountDownLatch
        CountDownLatch latch = new CountDownLatch(divide.size());
        for (List<ExamInfo> examInfos:divide){
            pool.execute(()->{
                for(ExamInfo examInfo1:examInfos){
                    excelImportMapper.updateExamScoreById(examInfo1);//通过Id更新
                }
                latch.countDown();
            });
            //必须执行完上面才能往下执行
            latch.await();
        }*/
        return Result.success();
    }
    /**
     * 导入execl文件批量更新面试分数
     * @param file
     * @throws IOException
     */
    @Override
    public Result<String> excelImportIntv(MultipartFile file) throws IOException {
        List<IntvInfo> intvInfoList = new ArrayList<>();
        //根据路径获取这个操作excel表格
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        //根据页面index 获取sheet页
        XSSFSheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 2; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            List<Object> list = null;
            if (row != null) {
                list = new ArrayList<>();
                for (Cell cell : row) {
                    if (cell != null) {
                        if (CellType.STRING.equals(cell.getCellType())) {
                            cell.setCellType(CellType.STRING);
                            String value = cell.getStringCellValue();
                            if (value != null && !"".equals(value))
                                list.add(value);
                        }
                        else{
                            cell.setCellType(CellType.NUMERIC);
                            Integer value = (int) cell.getNumericCellValue();
                            if (value != null && !"".equals(value))
                                list.add(value);
                        }
                    }
                    if (row.getCell(9).getCellType()!=CellType.NUMERIC){
                        return Result.error("上传失败，第"+i+"行第9列数据格式不对,应当为数字类型");
                    }
                }
                if (!list.isEmpty() && row.getCell(0) != null) {
                    IntvInfo intvInfo = new IntvInfo();
                    try{
                        intvInfo.setStudentId(row.getCell(0).getStringCellValue());
                        if(StringUtils.isEmpty(row.getCell(8).getStringCellValue()))
                            if (StringUtils.isEmpty(row.getCell(9).getNumericCellValue()))
                                intvInfo.setIntvState("已面试");
                        if (StringUtils.isEmpty(row.getCell(9).getNumericCellValue()))
                            intvInfo.setIntvScore((int) row.getCell(9).getNumericCellValue());
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    intvInfoList.add(intvInfo);
                    System.out.println(intvInfoList);
                }
            }
        }
        for (IntvInfo intvInfo:intvInfoList){
            excelImportMapper.updateIntvScoreById(intvInfo);//通过Id更新
        }
        return Result.success();
    }
}

